#include "MapObject.h"
int MapObject::write_name  = 0;
int MapObject::posit_name = 0; 
int MapObject::map_name = 0; 
int MapObject::pos_name  = 0;  
int MapObject::pos_tar_name  = 0; 
int MapObject::score_name = 0;
bool MapObject::compiled = false;

MapObject::MapObject()
{
}

MapObject::~MapObject(){}


void MapObject::LoadLists()
{
  //assegno i nomi alle liste
write_name = glGenLists(6);
posit_name = write_name+1;
map_name = posit_name +1;
pos_name = map_name +1; 
pos_tar_name = pos_name +1;
score_name = pos_name +2;

//posiziono la mappa
glNewList(posit_name,GL_COMPILE);
    glLoadIdentity();
    glTranslatef(-4.5f,12.0f,24);
    glRotatef(-26.0f,1,0,0);
    glTranslatef(-0.2,-3.8,0);
glEndList();

//scritta a video ''esc
glNewList(write_name,GL_COMPILE);
    glColor3fv(MYVOILET);     
    glRasterPos3f(0.1,-0.3,0);
    for (char *c=HELP_STRING; *c != '\0'; c++) 
        glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, *c);
    
     
    glRasterPos3f(0.1,7,0);
    for (char *c=TIME_STRING; *c != '\0'; c++) 
        glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, *c);   
        
glEndList();

//scritta a video punteggio
glNewList(score_name,GL_COMPILE);
    glColor3fv(MYVOILET);     
    glRasterPos3f(1.85,1.4,0);
    for (char *c=SCORE_STRING; *c != '\0'; c++) 
        glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, *c);
    
    
glEndList();

//abilito blending disegno mappa 
glNewList(map_name,GL_COMPILE);
        glColor4f(0.50f, 1.0f, 1.0f, 0.8);     
         glBlendFunc(GL_ONE,GL_ONE);         
        glEnable(GL_BLEND);         
        glEnable(GL_TEXTURE_2D);
        glBindTexture(GL_TEXTURE_2D,state->th->texNames[TEX_MAP]);
        
        glBegin(GL_QUADS);
          glTexCoord2f(0.0f, 1.0f);          glVertex3f(0, 0,0);
          glTexCoord2f(0.0f, 0.0f);          glVertex3f(0, 2,0);
           glTexCoord2f(1.0f, 0.0f);          glVertex3f(2, 2,0);
          glTexCoord2f(1.0f, 1.0f);          glVertex3f(2, 0,0);
        glEnd();
        glTranslatef(0.9,0.9,0.02);
glEndList();



glNewList(pos_tar_name,GL_COMPILE);
        glBindTexture(GL_TEXTURE_2D,state->th->texNames[TEX_BER]);
        glColor4f(1, 1.0f, 0.0f, 1);     
        glBegin(GL_QUADS);
          glTexCoord2f(0.0f, 1.0f);   glVertex3f(-0.1,-0.1,0);
              glTexCoord2f(0.0f, 0.0f);   glVertex3f(-0.1,0.1,0);
              glTexCoord2f(1.0f, 0.0f);  glVertex3f(.1,.1,0);
              glTexCoord2f(1.0f, 1.0f);   glVertex3f(0.1,-0.1,0);
            glEnd();
     
glEndList();


//disegno il puntatore dell'elicottero
glNewList(pos_name,GL_COMPILE);
        glBindTexture(GL_TEXTURE_2D,state->th->texNames[TEX_ELI]);
            glColor4f(1, 1.0f, 0.0f, 1);
            glRotatef(180,0,0,1);     
            glBegin(GL_QUADS);
              glTexCoord2f(0.0f, 1.0f);   glVertex3f(-0.1,-0.1,0);
              glTexCoord2f(0.0f, 0.0f);   glVertex3f(-0.1,0.1,0);
              glTexCoord2f(1.0f, 0.0f);  glVertex3f(.1,.1,0);
              glTexCoord2f(1.0f, 1.0f);   glVertex3f(0.1,-0.1,0);
            glEnd();
         
      glDisable(GL_TEXTURE_2D);
      glDisable(GL_BLEND); 
glEndList();
compiled = true;
}
void MapObject::DoIdle()
{
    time_t t2;
    time(&t2);
    
    double diff = difftime(t2,tempo);
    
    //lampeggio la scritta
    if ( diff >= 0.2) //secondi
      {
       if (write_enabled) write_enabled = false;
       else  write_enabled = true;
       time(&tempo);  
      }
}
void MapObject::Init()
{
    write_enabled = true;
    score=0; 
    time(&tempo);
    if (!compiled) LoadLists();
}
void MapObject::Draw(){
if (visible){    

glPushMatrix();
 
    //posiziono l'oggetto 
    glCallList(posit_name);

    //scritta per pausa 
    if(write_enabled)
    {
       // if (state->luce == 1) glDisable(GL_LIGHTING);    
        glCallList(write_name);
        
        //formatto  il tempo da mettere a video
        char o[20];  
        int b = state->int_time;
        int c = b % 60;
        b = (b - c)/60;
        
        if (c >= 10)      sprintf(o,"  %d:%d",b,c);
        else              sprintf(o,"  %d:0%d",b,c);
        
        for (char *c=o; *c != '\0'; c++) 
           glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24, *c);
        
        glCallList(score_name);
        
        //punteggio di gioco  
        char p[20];  
        sprintf(p,"%d",state->score);
        
        for (char *c=p; *c != '\0'; c++) 
           glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24, *c);
        
       
       // if (state->luce == 1) glEnable(GL_LIGHTING);   
    }

    //disegn o la mappa;
    glCallList(map_name);
    
    
  
    
    //disegno il puntatore al bersaglio
    float xmap = (state->targ->getX() * 2) / (TERRAIN_POINTS * TERRAIN_PART_DIM);
    float zmap = -(state->targ->getZ() * 2) / (TERRAIN_POINTS * TERRAIN_PART_DIM);
    
    glTranslatef(xmap+0.1,zmap+0.1,0);
    glCallList(pos_tar_name);
    glTranslatef(-xmap-0.1,-zmap-0.1,0);
            
    //calcolo la posizione dell'elicottero
    xmap = -(state->cam_posX * 2) / (TERRAIN_POINTS * TERRAIN_PART_DIM);
    zmap = (state->cam_posZ * 2) / (TERRAIN_POINTS * TERRAIN_PART_DIM);
    glTranslatef(xmap + 0.1,zmap + 0.1,.01);
  
    //disegno il puntatore
    glRotatef(-state->cam_angY,0,0,1);
    glCallList(pos_name);
   

glPopMatrix();
}
}
